/*
Mem.Get("key"); //获取值
Mem.Put("key","值支持对象更新部分","时间"); //更新值 可以只更新时间 时间0不过期 值不存在返回 false
Mem.Set(key,value,"时间");//设置或者新增值 
Mem.Add(key,"默认值1",time);//增加值  支持时间过期
Mem.Cut(key,"默认值1",time);//减少值 支持时间过期
DMem.Del(key);//删除
DMem.Flush(key);// key 为空清理全部  不为空删除包含关键词
*/
var Kuo_Cache_Memcached = {
    DB:null,
    //配置参数 Mode("Tools").Time()
    Config:async (config)=>{
        var Memcached = require('memcached');
        if(config){
            Kuo_Cache_Memcached.DB = new Memcached(config.Server,config.options);
        }else{
            Kuo_Cache_Memcached.DB = new Memcached(["127.0.0.1:11211"],{timeout:1000,poolSize:50});
        }
       
        return Kuo_Cache_Memcached;
    },Get:async (key)=>{
        /*获取数据 */
        return new Promise((resolve, reject) => {
            Kuo_Cache_Memcached.DB.get(key,function (err, data) {   
                if(err){
                    resolve(null);
                    return false;
                }
                if(data){
                    resolve(data);
                    return true;
                }
                resolve(null);
                return false;
            });
        });

    },Put:async (key,value,time)=>{
        /*更新*/
        if(!key){
            return false;
        }
        if(!time){
            time = 0;
        }
        var zhi =  await Kuo_Cache_Memcached.Get(key);
        if(value){
            if(zhi && typeof(zhi) == typeof(value)){
                for(var k in value){
                    zhi[k] = value[k];
                }
                value = zhi;
            }
        }else{
            value = zhi;
        }
        return await Kuo_Cache_Memcached.Set(key,value,time);
    },Set:async (key,value,time)=>{
        /**设置值  */
        if(!key){
            return false;
        }
        if(!time){
            time = 0;
        }
        return new Promise((resolve, reject) => { 
            Kuo_Cache_Memcached.DB.set(key, value, time, function (err) { 
                if(err){
                    resolve(null);
                    return false;
                }
                resolve(true);
                return true;
            });
        });

    },Add:async (key,value,time)=>{
        /* 增加 */
        if(!time){
            time = 0;
        }
        if(!value){
            value = 1;
        }
        var zhi = Number(await Kuo_Cache_Memcached.Get(key));
        if(!zhi){
            zhi = 0;
        }
        if(isNaN(zhi)){
            zhi = 0;
        }
        zhi = Number(zhi*1);
        zhi += Number(value);
        await Kuo_Cache_Memcached.Set(key,zhi,time);
        return zhi;

    },Cut:async (key,value,time)=>{
        /* 减少 */
        if(!time){
            time = 0;
        }
        if(!value){
            value = 1;
        }
        var zhi = Number( await Kuo_Cache_Memcached.Get(key));
        if(!zhi){
            zhi = 0;
        }
        if(isNaN(zhi)){
            zhi = 0;
        }
        zhi = Number(zhi*1);
        zhi -= Number(value);
        await Kuo_Cache_Memcached.Set(key,zhi,time);
        return zhi;
        
    },Del:async (key)=>{
        /*删除缓存 */
        return new Promise((resolve, reject) => { 
            Kuo_Cache_Memcached.DB.del(key, function (err) { 
                if(err){
                    resolve(false);
                    return false;
                }
                resolve(true);
                return true;
            });
        });
    },Total:async ()=>{
        /*统计数据 */
        return new Promise((resolve, reject) => { 
            Kuo_Cache_Memcached.DB.stats(function(err,y){
                if(err){
                    resolve(0);
                    return 0;
                }
                resolve(y[0].curr_items);
                return y[0].curr_items;
            });
        });
        
    },Flush:async (key)=>{
        /*清空数据 */
        return new Promise((resolve, reject) => { 
            Kuo_Cache_Memcached.DB.flush(function(err,y){
                resolve(true);
                return true;
            });
        });
    },Save:async ()=>{
        /* 预留接口存档 */
        return true;
    }

};
module.exports = Kuo_Cache_Memcached;